package lunadevs.luna.module.exploits;

import com.darkmagician6.eventapi.EventTarget;
import com.zCore.Core.zCore;

import lunadevs.luna.category.Category;
import lunadevs.luna.events.BoundingBoxEvent;
import lunadevs.luna.events.InsideBlockRenderEvent;
import lunadevs.luna.events.PushOutOfBlocksEvent;
import lunadevs.luna.module.Module;
import lunadevs.luna.utils.ClientUtils;
import lunadevs.luna.utils.MoveUtils;
import net.minecraft.block.Block;
import net.minecraft.block.BlockAir;
import net.minecraft.block.BlockHopper;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiMainMenu;
import net.minecraft.client.gui.GuiScreenAddServer;
import net.minecraft.client.gui.GuiSleepMP;
import net.minecraft.network.play.client.C03PacketPlayer;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.util.BlockPos;
import net.minecraft.util.MathHelper;
import net.minecraft.util.Timer;
import net.minecraft.world.WorldSettings.GameType;

public class PacketFly extends Module{

	public PacketFly() {
		super("PacketFly", 0, Category.EXPLOITS, true);
	}
	
	int clock = 0; /** Clock to fix the "Too many packets!" Kick, Works 99% of the time [NOT on TestNCP servers tho] */
	
	
	@Override
	public void onUpdate(){
		if(!this.isEnabled) return;
		float forward = 0.0F;
        float strafe = 0.0F;
        double speed = 0.2D;
       
        float var5 = MathHelper.sin(Minecraft.thePlayer.rotationYaw * 3.1415927F / 180.0F);
        float var6 = MathHelper.cos(Minecraft.thePlayer.rotationYaw * 3.1415927F / 180.0F);
        if(Minecraft.getMinecraft().gameSettings.keyBindAttack.pressed || Minecraft.getMinecraft().gameSettings.keyBindBack.pressed || Minecraft.getMinecraft().gameSettings.keyBindSneak.pressed || Minecraft.getMinecraft().gameSettings.keyBindForward.pressed || Minecraft.getMinecraft().gameSettings.keyBindJump.pressed){
            //Minecraft.getMinecraft().playerController.setGameType(GameType.CREATIVE); Replaced with Clock
        }else{
            if(Minecraft.getMinecraft().gameSettings.keyBindAttack.pressed==false || Minecraft.getMinecraft().gameSettings.keyBindBack.pressed==false || Minecraft.getMinecraft().gameSettings.keyBindSneak.pressed==false || Minecraft.getMinecraft().gameSettings.keyBindForward.pressed==false || Minecraft.getMinecraft().gameSettings.keyBindJump.pressed==false){
                //Minecraft.getMinecraft().playerController.setGameType(GameType.SURVIVAL); Replaced with Clock
            }
        }
       
        if (this.mc.gameSettings.keyBindForward.pressed) {
              forward += 0.1F;
            }
            if (this.mc.gameSettings.keyBindBack.pressed) {
              forward -= 0.1F;
            }
            if (this.mc.gameSettings.keyBindLeft.pressed) {
              strafe += 0.1F;
            }
            if (this.mc.gameSettings.keyBindRight.pressed) {
              strafe -= 0.1F;
            }
        /**
         * Go over holes smoother without lagging everywhere
         */
        if(Minecraft.getMinecraft().gameSettings.keyBindForward.pressed || Minecraft.getMinecraft().gameSettings.keyBindLeft.pressed || Minecraft.getMinecraft().gameSettings.keyBindRight.pressed || Minecraft.getMinecraft().gameSettings.keyBindBack.pressed) {
            zCore.p().motionX = zCore.p().posX * 8.0000000501;
            speed = 0.6952F; //To speed up
            isInsideBlock();
            zCore.p().motionZ = zCore.p().posZ * 8.0000000501;
            zCore.p().motionZ = zCore.p().posZ * 0.0000000501;
            zCore.p().motionZ = zCore.p().posZ * 0.0000000501;
            zCore.p().motionX = zCore.p().posX * 0.0000000501;
            zCore.p().motionZ = zCore.p().posZ * 0.0000000501;
            zCore.p().motionX = zCore.p().posX * 0.0000000501;
            zCore.p().motionZ = zCore.p().posZ * 0.0000000501;
            zCore.p().motionX = zCore.p().posX * 0.0000000501;
            zCore.p().motionZ = zCore.p().posZ * 0.0000000501;
            zCore.p().motionX = zCore.p().posX * 0.0000000501;
            zCore.p().motionZ = zCore.p().posZ * 0.0000000501;
            zCore.p().motionX = zCore.p().posX * 0.0000000501;
            zCore.p().motionZ = zCore.p().posZ * 0.0000000501;
            zCore.p().motionX = zCore.p().posX * 0.0000000501;
            zCore.p().motionZ = zCore.p().posZ * 0.0000000501;
            zCore.p().motionX = zCore.p().posX * 0.0000000501;
            zCore.p().motionZ = zCore.p().posZ * 0.0000000501;
            zCore.p().motionX = zCore.p().posX * 0.0000000501;
            zCore.p().motionZ = zCore.p().posZ * 0.0000000501;
            zCore.p().motionX = zCore.p().posX * 0.0000000501;
            zCore.p().motionZ = zCore.p().posZ * 0.0000000501;
            zCore.p().motionX = zCore.p().posX * 0.0000000501;
            zCore.p().motionX = zCore.p().posX * 0.0000000501;
           
           
            double motionX = (strafe * var6 - forward * var5) * speed;
            double motionZ = (forward * var6 + strafe * var5) * speed;
            Minecraft.thePlayer.motionX = motionX;
            Minecraft.thePlayer.motionZ = motionZ;
           
            /**
             * Be really careful with changing these numbers.
             */
        }
  	  
        zCore.p().motionY = 0f;
       /**
        * Update 3.1: Sends less packets [clock]
        */
       this.clock += 1;
       if (this.clock >= 10)
    {
           
        zCore.sendPacket(new C03PacketPlayer.C06PacketPlayerPosLook(zCore.p().posX + zCore.p().motionX, zCore.p().posY + (Minecraft.getMinecraft().gameSettings.keyBindJump.pressed ? 0.0622 : 0) - (Minecraft.getMinecraft().gameSettings.keyBindSneak.pressed ? 0.0622 : 0), zCore.p().posZ + zCore.p().motionZ, zCore.p().rotationYaw, zCore.p().rotationPitch, false));
        zCore.sendPacket(new C03PacketPlayer.C06PacketPlayerPosLook(zCore.p().posX + zCore.p().motionX, zCore.p().posY - 0999f, zCore.p().posZ + zCore.p().motionZ, zCore.p().rotationYaw , zCore.p().rotationPitch, true));
        this.clock = 0;
}
}
	
	 @EventTarget
	  public void onBB(BoundingBoxEvent event)
	  {
	    if ((isInsideBlock()) && (event.getBoundingBox() != null) && (event.getBoundingBox().maxY > ClientUtils.player().boundingBox.minY)) {
	      event.setBoundingBox(null);
	    }
	  }
	  
	  @EventTarget
	  public void onPush(PushOutOfBlocksEvent event)
	  {
	    event.setCancelled(true);
	  }
	  
	  @EventTarget
	  private void onInsideBlockRender(InsideBlockRenderEvent event)
	  {
	    event.setCancelled(true);
	  }
	private boolean isInsideBlock() {
		if(this.isEnabled==true && mc.thePlayer.isCollidedHorizontally){
		for (int x = MathHelper.floor_double(ClientUtils.player().boundingBox.minX); x < MathHelper.floor_double(ClientUtils.player().boundingBox.maxX) + 1; x++) {
	  	      for (int y = MathHelper.floor_double(ClientUtils.player().boundingBox.minY); y < MathHelper.floor_double(ClientUtils.player().boundingBox.maxY) + 1; y++) {
	  	        for (int z = MathHelper.floor_double(ClientUtils.player().boundingBox.minZ); z < MathHelper.floor_double(ClientUtils.player().boundingBox.maxZ) + 1; z++)
	  	        {
	  	          Block block = ClientUtils.world().getBlockState(new BlockPos(x, y, z)).getBlock();
	  	          if ((block != null) && (!(block instanceof BlockAir)))
	  	          {
	  	            AxisAlignedBB boundingBox = block.getCollisionBoundingBox(ClientUtils.world(), new BlockPos(x, y, z), ClientUtils.world().getBlockState(new BlockPos(x, y, z)));
	  	            if ((block instanceof BlockHopper)) {
	  	              boundingBox = new AxisAlignedBB(x, y, z, x + 1, y + 1, z + 1);
	  	            }
	  	            if (boundingBox != null) {
	  	              if (ClientUtils.player().boundingBox.intersectsWith(boundingBox)) {
	  	              }
	  	            return true;
	  	            }}
	  	          }
	  	        }
	  	      }
	  	    }
		return false;
	  	  }
	@Override
	public void onDisable() {
		//mc.displayGuiScreen(new GuiSleepMP());
		Minecraft.getMinecraft().playerController.setGameType(GameType.SURVIVAL);
		//mc.displayGuiScreen(new GuiMainMenu());
		super.onEnable();
	}
	
	public String getValue() {
		return "Testing";
	}
	
}
